package com.exam.api.config.validation.validator;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext;



public class TelValidator implements ConstraintValidator<Tel, String> {

    @Override
    public void initialize(Tel tel) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StrUtil.isEmpty(value)) {
            return true;
        }
        HibernateConstraintValidatorContext context = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class);
        if (!ReUtil.isMatch("^1[3-9]\\d{9}$", value)) {
            context.buildConstraintViolationWithTemplate("手机号格式错误").addConstraintViolation();
            return false;
        }
        return true;
    }
}
